﻿using Beauty.BLL;
using Beauty.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebUI.Areas.Admin.Models
{
    public class CheckUserFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (filterContext.HttpContext.Session[AdminDataConfig.UserSessionName] == null)
            {
                HttpCookie cookies = filterContext.HttpContext.Request.Cookies[AdminDataConfig.AutoLoginCookie];
                if (cookies == null)
                {
                    filterContext.HttpContext.Response.Redirect("~/Admin/Login");
                    return;
                }
                string uname = cookies.Values["name"];
                string upass = cookies.Values["pass"];
                userBLL bllUser = new userBLL();
                List<user> users = bllUser.GetModelList("UName='" + uname+"'");
                if ((uname == users[0].UName && upass == users[0].UPass))
                {
                    filterContext.HttpContext.Session[AdminDataConfig.UserSessionName] = users[0];
                }

            }
        }
    }
}